package com.nuoniu.sibanyun.controller.cm;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.cm.CmContractStage;
import com.nuoniu.sibanyun.entity.cm.CmContractStageGroup;
import com.nuoniu.sibanyun.service.cm.ICmContractStageGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 合同阶段组 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/cm/stageGroup")
@Api(tags = "合同阶段组管理")
public class CmContractStageGroupController extends BaseController {

    @Autowired
    private ICmContractStageGroupService stageGroupService;

    @ApiOperation("获取合同阶段组列表")
    @GetMapping("getList")
    public HttpResult getList(CmContractStageGroup contractStageGroup) {
        return HttpResult.ok(stageGroupService.getList(contractStageGroup));
    }

    @ApiOperation("获取详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(stageGroupService.getDetail(id));
    }

    @ApiOperation("保存合同阶段")
    @PostMapping("save")
    public HttpResult save(@RequestBody CmContractStageGroup stageGroup) {
        if(StringUtils.isBlank(stageGroup.getCode())) {
            throw new NuoNiuException("编码不能为空");
        }

        if(StringUtils.isBlank(stageGroup.getName())) {
            throw new NuoNiuException("名称不能为空");
        }

        if(stageGroup.getContractStageList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        return HttpResult.ok(stageGroupService.saveGroup(stageGroup));
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        if(id == null) {
            throw new NuoNiuException("删除失败");
        }

        stageGroupService.deleteById(id);
        return HttpResult.ok();
    }

}
